import request from '@/utils/request'

export interface BaseSaleAttrData {
  id: number,
  name: string
}



interface SpuSaleAttrValueData  {
  id?: number,
  spuId?: number,
  baseSaleAttrId: number | string,
  saleAttrValueName: string,
  saleAttrName?:string,
  isChecked?: null
}


export interface SpuSaleAttrData {
  id?: number,
  spuId?: number,
  baseSaleAttrId: number | string,
  saleAttrName: string,
  spuSaleAttrValueList: SpuSaleAttrValueData[],
  isEdit?:boolean,
  saleAttrValueName?:string,
  saleAttrIdValueId?:string
}

export interface ResponseData{
  data:string
}

export interface SpuImageData {
  id?: number,
  spuId?: number,
  imgName: string | undefined,
  imgUrl: string,
  name?:string,
  url?:string,
  response?:ResponseData,
  isDefault?:string
}

export type SpuData = {
  id?: number,
  spuName: string,
  description: string,
  category3Id: number | string,
  tmId: number | string,
  spuSaleAttrList: SpuSaleAttrData[],
  spuImageList: SpuImageData[]
}

type SpuPageListData = {
  records:SpuData [],
  total: number,
  size: number,
  current: number,
  searchCount: boolean,
  pages: number
}


export default {
  /**
   * @description 获取所有的销售属性列表
   * @url http://39.98.123.211:8510/admin/product/baseSaleAttrList
   * @returns Promise<BaseSaleAttrData[]>
   */
  getBaseSaleAttrList(){
    return request.get<any,BaseSaleAttrData[]>('/admin/product/baseSaleAttrList')
  },


  /**
   * @description 删除spu的操作
   * @url  http://39.98.123.211:8510/admin/product/deleteSpu/{spuId}
   * @param spuId 
   * @returns Promise<null>
   */
  deleteSpu(spuId:number){
    return request.delete<any,null>(`/admin/product/deleteSpu/${spuId}`)
  },
  

  /**
   * @description 获取spu基本信息[根据spu的id，获取spu的详情信息,spu的列表内部本来就已经有了spu的数据信息，但是列表内部的spu信息不是详情信息;在spu的详情页面（添加和修改）还要根据spu的id，再发一次请求获取到的才是spu的详情信息]
   * @url http://39.98.123.211:8510/admin/product/getSpuById/{spuId}
   * @param spuId 
   * @returns Promise<SpuData>
   */
  getSpuById(spuId:number){
    return request.get<any,SpuData>(`/admin/product/getSpuById/${spuId}`)
  },

  
  /**
   * @description 保存和更改spu信息
   * @url http://39.98.123.211:8510/admin/product/saveSpuInfo或updateSpuInfo
   * @param spu 
   * @returns Promise<null>
   */
  addOrUpdate(spu:SpuData){
    return request.post<any,null>(`/admin/product/${spu.id?'update':'save'}SpuInfo`,spu)
  },

  
  /**
   * @description 获取spu的分页列表[还需要第三个参数category3Id，这个参数是一个query参数]
   * @param page 当前页码
   * @param limit 每页展示数量
   * @param category3Id 三级列表的id
   * @returns Promise<SpuPageListData>
   */
  getPageList(page:number,limit:number,category3Id:number){
    return request.get<any,SpuPageListData>(`/admin/product/${page}/${limit}?category3Id=${category3Id}`)
  }


  /* 通过关键字去搜索相应的spu信息，目前用不到 */
  // GET /admin/product/findSpuInfoByKeyword/{keyword}
  // findSpuInfoByKeyword

  /* 根据多个spu的id获取多个spu的详情，目前不用 */
  // POST /admin/product/inner/findSpuInfoBySpuIdList
  // findSpuInfoBySpuIdList 
}
